package com.mz.ozy.activity.shop;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.mz.ozy.R;
import com.mz.ozy.activity.home.HomeActivity;
import com.mz.ozy.activity.home.HomeActivity1;
import com.mz.ozy.activity.me.MsgActivity;
import com.mz.ozy.activity.me.ObShopActivity;
import com.mz.ozy.adapter.MallBannerAdapter;
import com.mz.ozy.adapter.SearchListAdapter;
import com.mz.ozy.adapter.SortPopAdapter;
import com.mz.ozy.adapter.SortPopTwoAdapter;
import com.mz.ozy.adapter.TitleAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AccountBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.FriendBean;
import com.mz.ozy.bean.GoodsCategoryBean;
import com.mz.ozy.bean.GoodsListBean;
import com.mz.ozy.bean.HomeBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.ObShopBean;
import com.mz.ozy.bean.TwoTitleBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.MeModel;
import com.mz.ozy.mvp.presenter.GoodsListPresenter;
import com.mz.ozy.mvp.view.GoodsListContract;
import com.mz.ozy.mvp.view.MeContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.CustomPopWindow;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.sobot.chat.ZCSobotApi;
import com.sobot.chat.api.model.Information;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

/**
 * 五星精选
 */
public class SortSearchActivity extends BaseActivity<GoodsListContract.View, GoodsListPresenter> implements GoodsListContract.View {

    private MeModel mModel;
    CustomPopWindow mGridPopWindow;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    //    @BindView(R.id.iv_bannerPager)
//    ImageView ivBannerPager;
//    @BindView(R.id.rl_bannerPager)
//    RelativeLayout rlBannerPager;
    @BindView(R.id.tv_default)
    TextView mTvDefault;
    @BindView(R.id.tv_sales_volume)
    TextView mTvSalesVolume;
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.gridview)
    RecyclerView mGridview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.dragview)
    ImageView mDragview;
//    @BindView(R.id.customerserviceview)
//    ImageView mCustomerServiceView;
    @BindView(R.id.rele)
    RelativeLayout rele;
    @BindView(R.id.ll_sort_tab)
    LinearLayout mLlSortTab;
    @BindView(R.id.logoIv)
    ImageView logoIv;

    @BindView(R.id.rl_all_dynamic)
    RelativeLayout mLlAllDynamic;
    @BindView(R.id.rl_praised_dynamic)
    RelativeLayout mLlPraisedDynamic;
    @BindView(R.id.rl_praise_dynamic)
    RelativeLayout mLlPraiseDynamic;

    @BindView(R.id.tv_all_dynamic)
    TextView mTvAllDynamic;
    @BindView(R.id.tv_praised_dynamic)
    TextView mTvPraisedDynamic;
    @BindView(R.id.tv_praise_dynamic)
    TextView mTvPraiseDynamic;

    @BindView(R.id.view_all_dynamic)
    View mViewAllDynamic;
    @BindView(R.id.view_praised_dynamic)
    View mViewPraisedDynamic;
    @BindView(R.id.view_praise_dynamic)
    View mViewPraiseDynamic;
    TextView mTvCurrent;
    View mViewCurrent;

    //增加二级分类
//    @BindView(R.id.tab_title)
    SlidingTabLayout tab_title;
    @BindView(R.id.vp_none)
    ViewPager vp_none;

    private List<GoodsListBean.GoodsPageBean.PageItemsBean> mBeanList = new ArrayList<>();
    //private List<ObShopBean.PBean.PageItemsBean> mBeanList = new ArrayList<>();
    private SearchListAdapter mAdapter;
    private int tag;
    private int page = 1;
    private String id = ""; //进页面获取的是一级分类id，后续赋值会变成二级分类id
    private String topId = "";//一级分类的id
    private String type = "";
    private String amtType = "0";
    private String orderBy = "0";//按照什么排序
    private String orderType = "0";//0-升序 1-倒序
    private String name;
    private List<GoodsCategoryBean.CateListBean> mCateList;
    private String content;

    private int flag = 0;
    private float mOriginalX, mOriginalY;//手指按下时的初始位置
    private float lastX, lastY; //最后一次触摸事件的坐标
    private int containerWidth, containerHeight;    //容器宽高

    private TitleAdapter titleAdapter = null;
    private ArrayList<TwoTitleBean.MapListBean> titleList = new ArrayList<>();
    private MeBean mMeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_search);
        ButterKnife.bind(this);
        //mTitleTv.setVisibility(View.GONE);
        tab_title = findViewById(R.id.tab_title);
        initTitle();
        id = getIntent().getStringExtra("id");
        topId = id;
        type = getIntent().getStringExtra("type");
        name = getIntent().getStringExtra("name");
        content = getIntent().getStringExtra("content");

        if (!TextUtils.isEmpty(name)) {
            tvTitle.setText(name);
        } else if (!TextUtils.isEmpty(content)) {
            tvTitle.setText(content);
        }
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.mipmap.icon_right_search);
//        initView();
        getMeInfo(this);
        initLottery();
        if ("0".equals(type)) {
            mPresenter.getGoodsCategory(this);
        } else {
            logoIv.setVisibility(View.GONE);
        }
        mTvCurrent = mTvAllDynamic;
        mViewCurrent = mViewAllDynamic;

        if (content != null && !content.isEmpty()) {
            //搜索过来的
            initView();
        } else {
            mPresenter.getTwoTitle(this, id);
        }
    }

    //获取用户信息，用于进入客服
    private void getMeInfo(Activity activity) {
        mModel = new MeModel();
        mModel.getMeInfo(activity)
                .compose(RxUtil.rxSchedulerHelper(this.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<MeBean>(this) {
                    @Override
                    public void onSuccesss(MeBean bean) {
                        mMeBean = bean;
                    }
                });
    }

    //是否添加全部
    private boolean isAdd = false;

    //增加二级标签
    private void initTwoTitle(TwoTitleBean bean) {
//        titleList.add("1");
//        titleList.add("2");
//        titleList.add("3");
//        titleList.add("4");
//        titleList.add("5");
        titleList = bean.getMapList();

        TwoTitleBean.MapListBean all = new TwoTitleBean.MapListBean();
        all.setId("," + topId);
        all.setName("全部");

        titleList.add(0, all);
        titleAdapter = new TitleAdapter(this, titleList);
        vp_none.setAdapter(titleAdapter);


        tab_title.setViewPager(vp_none);
        tab_title.setSnapOnTabClick(true);
        if (titleList.size() > 0)
            id = titleList.get(0).getId();
        tab_title.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                tag = 0;
                page = 1;
                id = titleList.get(position).getId();
                mPresenter.onSearchGood(SortSearchActivity.this, id, type, amtType, orderBy, orderType, page + "");
                //mPresenter.getListData(SortSearchActivity.this, page + "", orderBy + "", type + "", content, orderType + "");
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


//        titleAdapter.setData((ArrayList<TwoTitleBean.MapListBean>) bean.getMapList());
//        titleAdapter.notifyDataSetChanged();
//        tab_title.getTitleView(0).setTypeface(Typeface.DEFAULT_BOLD);

        initView();

    }

    private void initLottery() {
        rele.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                //不能直接在onCreate()方法中得到宽高,会得到0,所以注册视图树的观察者来得到宽高
                //OnPreDrawListener是当一个视图树将要绘制时，所要调用的回调函数的接口类
                containerWidth = rele.getWidth();
                containerHeight = rele.getHeight();
                return true;
            }
        });

        mDragview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //记录触摸时的坐标,这里为什么要用getRawX()和getRawY()相信理解getX(),getY()和getRawX(),getRawY()的区别就知道为什么了
                        lastX = motionEvent.getRawX();
                        lastY = motionEvent.getRawY();
                        mOriginalX = motionEvent.getRawX();
                        mOriginalY = motionEvent.getRawY();
                        //return true对事件进行拦截,不继续下发,防止继续响应onClick事件.
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //每次移动的距离
                        float distanceX = motionEvent.getRawX() - lastX;
                        float distanceY = motionEvent.getRawY() - lastY;

                        //控件将要移动到的位置,先计算一下,不在ofFloat()方法中再计算是因为要防止控件移动到容器之外.
                        float nextX = mDragview.getX() + distanceX;
                        float nextY = mDragview.getY() + distanceY;
                        //如果将要移动到的 x 轴坐标小于0,则等于0,防止移出容器左边
                        if (nextX < 0)
                            nextX = 0;
                        //防止移出容器右边
                        if (nextX > containerWidth - mDragview.getWidth())
                            nextX = containerWidth - mDragview.getWidth();
                        //防止移出容器顶边
                        if (nextY < 0)
                            nextY = 0;
                        //防止移出容器底边
                        if (nextY > containerHeight - mDragview.getHeight())
                            nextY = containerHeight - mDragview.getHeight();
                        //利用属性动画改变控件的x,y坐标
                        ObjectAnimator mObjectAnimatorX = ObjectAnimator.ofFloat(mDragview, "x", mDragview.getX(), nextX);
                        ObjectAnimator mObjectAnimatorY = ObjectAnimator.ofFloat(mDragview, "y", mDragview.getY(), nextY);
                        AnimatorSet mAnimatorSet = new AnimatorSet();
                        mAnimatorSet.playTogether(mObjectAnimatorX, mObjectAnimatorY);
                        mAnimatorSet.setDuration(0);
                        mAnimatorSet.start();
                        //移动完之后记录当前坐标
                        lastX = motionEvent.getRawX();
                        lastY = motionEvent.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        if (Math.abs(motionEvent.getRawX() - mOriginalX) <
                                Utils.dp2px(SortSearchActivity.this, 3) &&
                                Math.abs(motionEvent.getRawY() - mOriginalY) <
                                        Utils.dp2px(SortSearchActivity.this, 3)) {
                            CarActivity.go(SortSearchActivity.this);
                        }
                        break;
                }
                return false;
            }
        });

//        mCustomerServiceView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                startActivity(new Intent(MsgActivity.this, CustomerServiceActivity.class));
//                Information info = new Information();
//                info.setApp_key("f970b1112b8447b6abe76c7acbf450cd");
//                info.setPartnerid(Constants.uid);
//                info.setUser_nick(mMeBean.getNickname());
//                info.setUser_name(mMeBean.getUsername());
//                info.setUser_tels(mMeBean.getUsername());
//                info.setFace(mMeBean.getUseravatar());
//                ZCSobotApi.openZCChat(SortSearchActivity.this, info);
//            }
//        });
//        mCustomerServiceView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                switch (motionEvent.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        //记录触摸时的坐标,这里为什么要用getRawX()和getRawY()相信理解getX(),getY()和getRawX(),getRawY()的区别就知道为什么了
//                        lastX = motionEvent.getRawX();
//                        lastY = motionEvent.getRawY();
//                        mOriginalX = motionEvent.getRawX();
//                        mOriginalY = motionEvent.getRawY();
//                        //return true对事件进行拦截,不继续下发,防止继续响应onClick事件.
//                        return true;
//                    case MotionEvent.ACTION_MOVE:
//                        //每次移动的距离
//                        float distanceX = motionEvent.getRawX() - lastX;
//                        float distanceY = motionEvent.getRawY() - lastY;
//
//                        //控件将要移动到的位置,先计算一下,不在ofFloat()方法中再计算是因为要防止控件移动到容器之外.
//                        float nextX = mCustomerServiceView.getX() + distanceX;
//                        float nextY = mCustomerServiceView.getY() + distanceY;
//                        //如果将要移动到的 x 轴坐标小于0,则等于0,防止移出容器左边
//                        if (nextX < 0)
//                            nextX = 0;
//                        //防止移出容器右边
//                        if (nextX > containerWidth - mCustomerServiceView.getWidth())
//                            nextX = containerWidth - mCustomerServiceView.getWidth();
//                        //防止移出容器顶边
//                        if (nextY < 0)
//                            nextY = 0;
//                        //防止移出容器底边
//                        if (nextY > containerHeight - mCustomerServiceView.getHeight())
//                            nextY = containerHeight - mCustomerServiceView.getHeight();
//                        //利用属性动画改变控件的x,y坐标
//                        ObjectAnimator mObjectAnimatorX = ObjectAnimator.ofFloat(mCustomerServiceView, "x", mCustomerServiceView.getX(), nextX);
//                        ObjectAnimator mObjectAnimatorY = ObjectAnimator.ofFloat(mCustomerServiceView, "y", mCustomerServiceView.getY(), nextY);
//                        AnimatorSet mAnimatorSet = new AnimatorSet();
//                        mAnimatorSet.playTogether(mObjectAnimatorX, mObjectAnimatorY);
//                        mAnimatorSet.setDuration(0);
//                        mAnimatorSet.start();
//                        //移动完之后记录当前坐标
//                        lastX = motionEvent.getRawX();
//                        lastY = motionEvent.getRawY();
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        if (Math.abs(motionEvent.getRawX() - mOriginalX) <
//                                Utils.dp2px(SortSearchActivity.this, 3) &&
//                                Math.abs(motionEvent.getRawY() - mOriginalY) <
//                                        Utils.dp2px(SortSearchActivity.this, 3)) {
//                            MsgActivity.go(SortSearchActivity.this);
//                        }
//                        break;
//                }
//                return false;
//            }
//        });
    }

    private void initView() {
        mGridview.setLayoutManager(new GridLayoutManager(this, 2));
        mAdapter = new SearchListAdapter(mBeanList);
        mGridview.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //商品详情
                final GoodsListBean.GoodsPageBean.PageItemsBean item = mAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_item_view: {
                        Intent intent = new Intent(SortSearchActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", item.getId() + "");
                        startActivity(intent);
                        break;
                    }
                    case R.id.iv_search_car: {
                        Intent intent = new Intent(SortSearchActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", item.getId() + "");
                        intent.putExtra("show", 1);
                        startActivity(intent);
                        break;
                    }
                }
            }
        });
        mSmartRefreshLayout.setEnableAutoLoadmore(true);
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tag = 1;
                page = page + 1;
                if (!TextUtils.isEmpty(id)) {
                    mPresenter.onSearchGood(SortSearchActivity.this, id, type, amtType, orderBy, orderType, page + "");
                    //mPresenter.getListData(SortSearchActivity.this, page + "", orderBy + "", type + "", content, orderType + "");
                } else if (!TextUtils.isEmpty(content)) {
                    mPresenter.onSearchGoodByKey(SortSearchActivity.this, content, type, amtType, orderBy, orderType, page + "");
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                searchListByRefresh();
            }
        });
        if (!TextUtils.isEmpty(id) || !TextUtils.isEmpty(content)) {
            mSmartRefreshLayout.autoRefresh();
        }
    }

    private void searchListByRefresh() {
        tag = 0;
        page = 1;
        if (!TextUtils.isEmpty(id)) {
            mPresenter.onSearchGood(SortSearchActivity.this, id, type, amtType, orderBy, orderType, page + "");
            //mPresenter.getListData(SortSearchActivity.this, page + "", orderBy + "", type + "", content, orderType + "");
        } else if (!TextUtils.isEmpty(content)) {
            mPresenter.onSearchGoodByKey(SortSearchActivity.this, content, type, amtType, orderBy, orderType, page + "");
        }
    }

    @OnClick({R.id.ll_title, R.id.ll_default, R.id.ll_sales_volume, R.id.ll_price
            , R.id.rl_all_dynamic, R.id.rl_praised_dynamic, R.id.rl_praise_dynamic
            , R.id.rightImage})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_title: {
                if ("0".equals(type)) {
                    if (mGridPopWindow != null) {
                        mGridPopWindow.showAsDropDown(titleBar);
                    } else {
                        showPopListView();
                    }
                }
                break;
            }
            case R.id.ll_default:    //默认
                onSelectType(0);
                break;
            case R.id.ll_sales_volume:    //销量
                onSelectType(1);
                break;
            case R.id.ll_price:      //价格
                onSelectType(2);
                break;
            case R.id.rl_all_dynamic:      //现金
                changeCurrentTab(mTvAllDynamic, mViewAllDynamic, 0);
                break;
            case R.id.rl_praised_dynamic:      //现金积分
                changeCurrentTab(mTvPraisedDynamic, mViewPraisedDynamic, 1);
                break;
//            case R.id.rl_praise_dynamic:      //积分
//                changeCurrentTab(mTvPraiseDynamic, mViewPraiseDynamic, 2);
//                break;
            case R.id.rightImage:       //搜索
                startActivity(new Intent(this, SearchActivity.class));
                finish();
                break;
        }
    }

    private void changeCurrentTab(TextView textView, View tabview, int type) {
        if (mTvCurrent != textView) {
            mTvCurrent.setTextColor(ContextCompat.getColor(SortSearchActivity.this, R.color.c_a5a5a5));
            mViewCurrent.setVisibility(View.INVISIBLE);
            textView.setTextColor(ContextCompat.getColor(SortSearchActivity.this, R.color.c_73b21a));
            tabview.setVisibility(View.VISIBLE);
            mTvCurrent = textView;
            mViewCurrent = tabview;
            this.amtType = String.valueOf(type);
            searchListByRefresh();
        }
    }

    public void onSelectType(int type) {
        mTvDefault.setTextColor(getResources().getColor(R.color.c_363636));
        mTvSalesVolume.setTextColor(getResources().getColor(R.color.c_363636));
        mTvPrice.setTextColor(getResources().getColor(R.color.c_363636));

        Drawable drawable2 = getResources().getDrawable(R.mipmap.sale_and_price_pic);
        // 这一步必须要做,否则不会显示.
        drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        mTvPrice.setCompoundDrawables(null, null, drawable2, null);
        if (type == 2) {
            orderBy = "2";
            mTvPrice.setTextColor(getResources().getColor(R.color.c_73b21a));
            if (TextUtils.isEmpty(orderType)) {
                orderType = "0";
                Drawable drawable6 = getResources().getDrawable(R.mipmap.sale_and_price_sx_pic);
                // 这一步必须要做,否则不会显示.
                drawable6.setBounds(0, 0, drawable6.getMinimumWidth(), drawable6.getMinimumHeight());
                mTvPrice.setCompoundDrawables(null, null, drawable6, null);

            } else if ("0".equals(orderType)) {
                orderType = "1";
                Drawable drawable6 = getResources().getDrawable(R.mipmap.sale_and_price_jx_pic);
                // 这一步必须要做,否则不会显示.
                drawable6.setBounds(0, 0, drawable6.getMinimumWidth(), drawable6.getMinimumHeight());
                mTvPrice.setCompoundDrawables(null, null, drawable6, null);
            } else if ("1".equals(orderType)) {
                orderType = "0";
                Drawable drawable6 = getResources().getDrawable(R.mipmap.sale_and_price_sx_pic);
                // 这一步必须要做,否则不会显示.
                drawable6.setBounds(0, 0, drawable6.getMinimumWidth(), drawable6.getMinimumHeight());
                mTvPrice.setCompoundDrawables(null, null, drawable6, null);
            }
            searchListByRefresh();
        } else if (type == 1) {
            orderBy = "1";
            orderType = "1";
            mTvSalesVolume.setTextColor(getResources().getColor(R.color.c_73b21a));
            searchListByRefresh();
        } else {
            orderBy = "0";
            orderType = "1";
            mTvDefault.setTextColor(getResources().getColor(R.color.c_73b21a));
            searchListByRefresh();
        }
    }

    private void showPopListView() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_brand_grid, null);
        //处理popWindow 显示内容
        handleGridView(contentView);
        //创建并显示popWindow
        mGridPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                .create()
                .showAsDropDown(titleBar);
    }


    private SortPopAdapter mBrandPopGridAdapter;

    //二级分类列表
    private RecyclerView gridviewTwo;
    private SortPopTwoAdapter mBrandPopGridAdapterTwo;

    private void handleGridView(View contentView) {
        RecyclerView gridview = contentView.findViewById(R.id.recyclerView);
        gridviewTwo = contentView.findViewById(R.id.recyclerViewTwo);
        gridview.setLayoutManager(new LinearLayoutManager(this));
        contentView.findViewById(R.id.view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mGridPopWindow != null) {
                    mGridPopWindow.dissmiss();
                }
            }
        });
        if (mCateList != null) {
            mBrandPopGridAdapter = new SortPopAdapter(mCateList, id);
            gridview.setAdapter(mBrandPopGridAdapter);
            mBrandPopGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                    //一级分类

                    final GoodsCategoryBean.CateListBean item = (GoodsCategoryBean.CateListBean) mBrandPopGridAdapter.getItem(position);
                    id = String.valueOf(item.getId());
                    topId = id;
                    name = item.getName();
                    mBrandPopGridAdapter.setSelectPosition(position);
                    mBrandPopGridAdapter.notifyDataSetChanged();
                    mPresenter.getTwoTitle(SortSearchActivity.this, id);
                }
            });
        }

        mPresenter.getTwoTitle(this, topId);

    }

    //搜索商品成功
    @Override
    public void getSearchGoodSuccess(GoodsListBean bean) {
        if (tag == 0) {
            mSmartRefreshLayout.setLoadmoreFinished(false);
            mSmartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getP().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mSmartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getP().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getP().getPageItems().size() == 0 || bean.getP().getPageSize() * bean.getP().getPageNumber() >= bean.getP().getPageTotle()) {
            mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    //获取商品列表
    @Override
    public void getGoodsCategoryData(GoodsCategoryBean bean) {
        final List<GoodsCategoryBean.CateListBean> cateList = bean.getCateList();
        mCateList = cateList;
        if (mCateList != null && mCateList.size() > 0) {
            if (TextUtils.isEmpty(content)) {
                if (TextUtils.isEmpty(id)) {
                    id = mCateList.get(0).getId() + "";
                    mSmartRefreshLayout.autoRefresh();
                }
                if (TextUtils.isEmpty(name)) {
                    name = mCateList.get(0).getName();
                    tvTitle.setText(name);
                }
            }
        }
    }

    @Override
    public void getGoodsTopicData(GoodsCategoryBean bean) {
        final List<GoodsCategoryBean.CateListBean> cateList = bean.getCateList();
        mCateList = cateList;
        if (mCateList != null && mCateList.size() > 0) {
            if (TextUtils.isEmpty(content)) {
                if (TextUtils.isEmpty(id)) {
                    id = mCateList.get(0).getId() + "";
                    mSmartRefreshLayout.autoRefresh();
                }
                if (TextUtils.isEmpty(name)) {
                    name = mCateList.get(0).getName();
                    tvTitle.setText(name);
                }
            }
        }
    }

    @Override
    public void getTwoTitleSuccess(TwoTitleBean bean) {
        //tab二级分类
        initTwoTitle(bean);

        initTopTwoList(bean);

    }

    @Override
    public void showListData(GoodsListBean bean) {
        if (tag == 0) {
            mSmartRefreshLayout.setLoadmoreFinished(false);
            mSmartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getP().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mSmartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getP().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getP().getPageItems().size() == 0 || bean.getP().getPageSize() * bean.getP().getPageNumber() >= bean.getP().getPageTotle()) {
            mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

//    @Override
//    public void getMyMeInfoSuccess(MeBean bean) {
//        mMeBean = bean;
//    }

    //头部列表二级分类
    private void initTopTwoList(TwoTitleBean bean) {
        if (gridviewTwo == null)
            return;
        ArrayList<TwoTitleBean.MapListBean> mapList = bean.getMapList();
        TwoTitleBean.MapListBean all = new TwoTitleBean.MapListBean();
        all.setId("," + topId);
        all.setName("全部");
//        mapList.add(0, all);

        mBrandPopGridAdapterTwo = new SortPopTwoAdapter(mapList);
        gridviewTwo.setLayoutManager(new LinearLayoutManager(this));
        gridviewTwo.setAdapter(mBrandPopGridAdapterTwo);

        mBrandPopGridAdapterTwo.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                tvTitle.setText(name);
                id = mapList.get(position).getId();
                tab_title.setCurrentTab(position);
                mSmartRefreshLayout.autoRefresh();
                mGridPopWindow.dissmiss();

            }
        });

    }
}
